﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using PictureManager.Core;

namespace PictureManager.WPF
{
    internal delegate TOUT AsyncFunctionCallDelegate<TIN, TOUT>(TIN args);
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        #region Fields
        private String RootNode;


        #endregion

        public Window1()
        {
            InitializeComponent();
            InitEvent();
            RootNode = PictureSettings.Instance.PictureRootNode;
            BindingTreeView(RootNode, treeViewPicture.Items);
        }
       
        private void InitEvent()
        {
            
        }
        private void BackgroundWorker(Boolean isBackgroundWork)
        {
            
        }
        
        
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (isCusipValid(this.textBox1.Text))
            {
                label1.Content = "Valid";
            }
            else
                label1.Content = "InValid";
        }
        private bool isCusipValid(string cusip)
        {
            if (cusip.Length == 6) return true;
            if (cusip.Length != 9)
                return false;
            Char[] chars = cusip.ToCharArray();
            Int32 sum, i, d;
            Boolean multiply = true;
            for (sum = 0, i = 7; i > -1; --i)
            {
                if (i < 3)
                {
                    if (chars[i] >= '0' && chars[i] <= '9')
                        d = chars[i] - '0';
                    else
                        return false;
                }
                else
                {
                    if (chars[i] >= 'A' && chars[i] <= 'Z')
                        d = chars[i] - 'A' + 10;
                    else if (chars[i] >= '0' && chars[i] <= '9')
                        d = cusip[i] - '0';
                    else
                        return false;
                }
                if (multiply)
                    d *= 2;
                multiply = !multiply;
                sum += (d / 10) + (d % 10);
            }
            sum %= 10;
            sum = 10 - sum;
            sum %= 10;

            if (sum != chars[8] - '0')
                return false;
            return true;
        }
    }
}
